package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostAreaDao;
import ysq.xyg.host.model.dto.HostAreaDto;
import ysq.xyg.host.model.po.HostArea;
import ysq.xyg.host.model.vo.HostAreaVo;
import ysq.xyg.host.service.HostAreaService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 房产-城市区域表service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:26:01
 */
@Service("hostAreaService")
public class HostAreaServiceImpl extends BaseServiceImpl<HostArea> implements HostAreaService {

    @Autowired
    private HostAreaDao hostAreaDao;

    @Autowired
    public void setBaseDao(HostAreaDao hostAreaDao) {
        this.baseDao = hostAreaDao;
    }

    /**
     * 分页查询
     *
     * @param hostAreaVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:26:01
     */
    @Override
    public ReturnEntity page(HostAreaVo hostAreaVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostArea> list = acceptList(hostAreaVo);
        PageInfo<HostArea> page = new PageInfo<HostArea>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostAreaVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:26:01
     */
    @Override
    public ReturnEntity addOrUpdate(HostAreaVo hostAreaVo, SysUser currentUser) {
        boolean isUpate = true;
        HostArea hostArea;
        if (StrUtil.isEmpty(hostAreaVo.getId())) {
            hostArea = new HostArea(currentUser);
            isUpate = false;
        } else {
            hostArea = selectByPrimaryKey(hostAreaVo.getId());
            if (ObjectUtil.isNull(hostArea)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostArea.setUpdateTime(new Date());
            hostArea.setUpdateUser(currentUser.getId());
        }
        hostArea.setCityId(hostAreaVo.getCityId());
        hostArea.setCityName(hostAreaVo.getCityName());
        hostArea.setName(hostAreaVo.getName());
        hostArea.setDescription(hostAreaVo.getDescription());
        hostArea.setOrderNum(hostAreaVo.getOrderNum());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostArea);
        } else {
            num = insert(hostArea);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:26:01
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostArea hostArea = selectByPrimaryKey(id);
        if (hostArea != null) {
            hostArea.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostArea.setUpdateTime(new Date());
            hostArea.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostArea);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostAreaVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:26:01
     */
    @Override
    public List<HostArea> acceptList(HostAreaVo hostAreaVo){
        Example example = new Example(HostArea.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostAreaVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostAreaVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostAreaVo.getAccountId())) {
            criteria.andEqualTo("accountId", hostAreaVo.getAccountId());
        }
        if (StringUtil.isNotEmpty(hostAreaVo.getName())) {
            criteria.andLike("name", "%" + hostAreaVo.getName() + "%");
        }
        if (StringUtil.isNotEmpty(hostAreaVo.getCityId())) {
            criteria.andEqualTo("cityId", hostAreaVo.getCityId());
        }
        example.setOrderByClause(" update_time desc");
        return hostAreaDao.selectByExample(example);
    }

    @Override
    public ReturnEntity acceptSelect(String code, String cityId, String accountId) {
        HostAreaVo hostAreaVo = new HostAreaVo();
        hostAreaVo.setAccountId(accountId);
        hostAreaVo.setCityId(cityId);
        List<HostArea> list = acceptList(hostAreaVo);
        List<SysDictContent> sysDictContentList = new ArrayList<>();
        for (HostArea entity : list) {
            SysDictContent sysDictContent = new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }

    @Override
    public List<HostAreaDto> selectHostAreaByCityIdList(String cityId) {
        return hostAreaDao.selectHostAreaByCityIdList(cityId);
    }
}
